package fzu.functiongather.BottomTab;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageButton;

import java.util.ArrayList;
import java.util.List;

import fzu.functiongather.R;


/**
 * Created by RuiyuHuang on 2016/7/14 0014.
 */
public class BottomTabActivity extends FragmentActivity implements View.OnClickListener{
    /**
     * ViewPager
     */
    private ViewPager mViewPager;

    /**
     * ViewPager的适配器
     */
    private FragmentPagerAdapter mAdapter;
    private List<Fragment> mFragments = new ArrayList<Fragment>();

    /**
     * 底部四个按钮
     */
    private ImageButton mTabBtnFirst;
    private ImageButton mTabBtnSecond;
    private ImageButton mTabBtnThird;
    private ImageButton mTabBtnFourth;

    /**
     * 四个自定义Fragment
     */
    TabFragmentFirst mTabFirst  = new TabFragmentFirst();
    TabFragmentSecond mTabSecond = new TabFragmentSecond();
    TabFragmentThird mTabThree  = new TabFragmentThird();
    TabFragmentFourth mTabFourth  = new TabFragmentFourth();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.bottom_tab_main);

        mViewPager = (ViewPager) findViewById(R.id.view_pager);

        initView();

        mAdapter = new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return mFragments.get(position);
            }

            @Override
            public int getCount() {
                return mFragments.size();
            }
        };

        mViewPager.setAdapter(mAdapter);

        mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

                resetTabBtn();

                switch (position) {
                    case 0:
                        ((ImageButton)mTabBtnFirst.findViewById(R.id.tab_btn_first))
                                .setImageResource(R.mipmap.tab_pressed);
                        break;
                    case 1:
                        ((ImageButton)mTabBtnSecond.findViewById(R.id.tab_btn_second))
                                .setImageResource(R.mipmap.tab_pressed);
                        break;
                    case 2:
                        ((ImageButton)mTabBtnThird.findViewById(R.id.tab_btn_third))
                                .setImageResource(R.mipmap.tab_pressed);
                        break;
                    case 3:
                        ((ImageButton)mTabBtnFourth.findViewById(R.id.tab_btn_fourth))
                                .setImageResource(R.mipmap.tab_pressed);
                        break;
                    default:
                        break;
                }
            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }

    /**
     * 重置按钮状态
     */
    protected void resetTabBtn() {
        ((ImageButton)mTabBtnFirst.findViewById(R.id.tab_btn_first))
                .setImageResource(R.mipmap.tab_normal);

        ((ImageButton)mTabBtnSecond.findViewById(R.id.tab_btn_second))
                .setImageResource(R.mipmap.tab_normal);

        ((ImageButton)mTabBtnThird.findViewById(R.id.tab_btn_third))
                .setImageResource(R.mipmap.tab_normal);

        ((ImageButton)mTabBtnFourth.findViewById(R.id.tab_btn_fourth))
                .setImageResource(R.mipmap.tab_normal);
    }

    /**
     * 初始化View
     */
    private void initView()
    {
        mTabBtnFirst = (ImageButton) findViewById(R.id.tab_btn_first);
        mTabBtnSecond = (ImageButton) findViewById(R.id.tab_btn_second);
        mTabBtnThird = (ImageButton) findViewById(R.id.tab_btn_third);
        mTabBtnFourth = (ImageButton) findViewById(R.id.tab_btn_fourth);

        mFragments.add(mTabFirst);
        mFragments.add(mTabSecond);
        mFragments.add(mTabThree);
        mFragments.add(mTabFourth);

        mTabBtnFirst.setOnClickListener(this);
        mTabBtnSecond.setOnClickListener(this);
        mTabBtnThird.setOnClickListener(this);
        mTabBtnFourth.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId())
        {
            case R.id.tab_btn_first:
                mViewPager.setCurrentItem(0,false);
                break;
            case R.id.tab_btn_second:
                mViewPager.setCurrentItem(1,false);
                break;
            case R.id.tab_btn_third:
                mViewPager.setCurrentItem(2,false);
                break;
            case R.id.tab_btn_fourth:
                mViewPager.setCurrentItem(3,false);
                break;
            default:
                break;
        }
    }

}
